# -*- coding: utf-8 -*-
# @Time : 2022/5/24 11:02
# @FileName: MonthDataCurve.py
# @Mail: lizhigen1996@aliyun.com

__author__ = 'Zhigen.li'

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QCursor, QColor
from PyQt5.QtWidgets import QToolTip, QDialog, QApplication, QGridLayout
from PyQt5.QtChart import QChart, QChartView, QBarSeries, QBarSet, QBarCategoryAxis, QValueAxis
from res.resource_rc import *
from Tools.Util import *
from Config.Config import CatchExceptions


# 月初净值月末净值对比
class MonthDataCurve_NavBar(QDialog):
    def __init__(self, name, code):
        super(MonthDataCurve_NavBar, self).__init__()
        self.resize(1000, 600)
        self.setWindowTitle('{}({}) 月初、月末单位净值对比'.format(name, code))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")

    @CatchExceptions
    def showCurve(self, x, y1, y2):
        self.x = x[::-1]
        self.y1 = y1[::-1]
        self.y2 = y2[::-1]

        series = QBarSeries()
        self.setTempZ = QBarSet('月初单位净值')
        self.setTempZ.hovered.connect(self.onBarSetClicked)
        self.setTempZ.append(self.y1)
        self.setTempZ.setColor(QColor('#E14745'))
        series.append(self.setTempZ)

        self.setTempF = QBarSet('月末单位净值')
        self.setTempF.hovered.connect(self.onBarSetClicked)
        self.setTempF.append(self.y2)
        self.setTempF.setColor(QColor('#3F931F'))
        series.append(self.setTempF)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("月初、月末单位净值对比")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        axisY = QValueAxis()
        axisY.applyNiceNumbers()
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        self.axis_x = QBarCategoryAxis()
        self.axis_x.append(self.x)
        self.axis_x.setLabelsAngle(75)
        chart.addAxis(self.axis_x, Qt.AlignBottom)
        series.attachAxis(self.axis_x)

        # 隐藏刻度线
        self.axis_x.setGridLineVisible(False)
        axisY.setGridLineVisible(False)

        chart.legend().setVisible(True)
        # chart.legend().setAlignment(Qt.AlignBottom)

        self.chartView = QChartView(chart)
        self.chartView.setRenderHint(QPainter.Antialiasing)
        self.gridLayout.addWidget(self.chartView, 0, 0, 1, 1)
        self.exec()

    @CatchExceptions
    def onBarSetClicked(self, status, index):
        if status:
            if self.sender() == self.setTempZ:
                pass
            if self.sender() == self.setTempF:
                pass
            text = "{}\n月初净值：{}元\n月末净值：{}元".format(self.x[index], self.y1[index], self.y2[index])

            QToolTip.showText(QCursor.pos(), text, self, self.rect(), 2000)

# 月最高涨、最低跌收益百分比
class MonthDataCurve_PerBar(QDialog):
    def __init__(self, name, code):
        super(MonthDataCurve_PerBar, self).__init__()
        self.resize(1000, 600)
        self.setWindowTitle('{}({}) 月最高涨、最低跌收益百分比'.format(name, code))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")

    @CatchExceptions
    def showCurve(self, x, y1, y2):
        self.x = x[::-1]
        self.y1 = y1[::-1]
        self.y2 = y2[::-1]

        series = QBarSeries()
        self.setTempZ = QBarSet('月最高涨')
        self.setTempZ.hovered.connect(self.onBarSetClicked)
        self.setTempZ.append(self.y1)
        self.setTempZ.setColor(QColor('#E14745'))
        series.append(self.setTempZ)

        self.setTempF = QBarSet('月最低跌')
        self.setTempF.hovered.connect(self.onBarSetClicked)
        self.setTempF.append(self.y2)
        self.setTempF.setColor(QColor('#3F931F'))
        series.append(self.setTempF)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("月最高涨、最低跌收益百分比")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        axisY = QValueAxis()
        axisY.applyNiceNumbers()
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        self.axis_x = QBarCategoryAxis()
        self.axis_x.append(self.x)
        self.axis_x.setLabelsAngle(75)
        chart.addAxis(self.axis_x, Qt.AlignBottom)
        series.attachAxis(self.axis_x)

        # 隐藏刻度线
        self.axis_x.setGridLineVisible(False)
        axisY.setGridLineVisible(False)

        chart.legend().setVisible(True)
        # chart.legend().setAlignment(Qt.AlignBottom)

        self.chartView = QChartView(chart)
        self.chartView.setRenderHint(QPainter.Antialiasing)
        self.gridLayout.addWidget(self.chartView, 0, 0, 1, 1)
        self.exec()

    @CatchExceptions
    def onBarSetClicked(self, status, index):
        if status:
            if self.sender() == self.setTempZ:
                pass
            if self.sender() == self.setTempF:
                pass
            text = "{}\n最高涨：{}%\n最低跌：{}%".format(self.x[index], self.y1[index], self.y2[index])

            QToolTip.showText(QCursor.pos(), text, self, self.rect(), 2000)

# 月波动值（最高涨、最低跌之差）
class MonthDataCurve_FluBar(QDialog):
    def __init__(self, name, code):
        super(MonthDataCurve_FluBar, self).__init__()
        self.resize(1000, 600)
        self.setWindowTitle('{}({})月波动值（最高涨、最低跌之差）'.format(name,code))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")

    @CatchExceptions
    def showCurve(self, x, y1, y2):
        self.x = x[::-1]
        self.y1 = y1[::-1]
        self.y2 = y2[::-1]
        self.y = [self.y1[idx] - self.y2[idx] for idx in range(len(self.y1))]

        series = QBarSeries()
        self.setTempZ = QBarSet('波动值')
        self.setTempZ.hovered.connect(self.onBarSetClicked)
        self.setTempZ.append(self.y)
        self.setTempZ.setColor(QColor('#E14745'))
        series.append(self.setTempZ)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("月波动值（最高涨、最低跌之差）")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        axisY = QValueAxis()
        axisY.applyNiceNumbers()
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        self.axis_x = QBarCategoryAxis()
        self.axis_x.append(self.x)
        self.axis_x.setLabelsAngle(75)
        chart.addAxis(self.axis_x, Qt.AlignBottom)
        series.attachAxis(self.axis_x)

        # 隐藏刻度线
        self.axis_x.setGridLineVisible(False)
        axisY.setGridLineVisible(False)

        chart.legend().setVisible(True)
        # chart.legend().setAlignment(Qt.AlignBottom)

        self.chartView = QChartView(chart)
        self.chartView.setRenderHint(QPainter.Antialiasing)
        self.gridLayout.addWidget(self.chartView, 0, 0, 1, 1)
        self.exec()

    @CatchExceptions
    def onBarSetClicked(self, status, index):
        if status:
            text = "{}\n波动值：{}%".format(self.x[index], self.y[index])

            QToolTip.showText(QCursor.pos(), text, self, self.rect(), 2000)

# 月净值差值(月末减月初，该月是否盈亏）
class MonthDataCurve_DifBar(QDialog):
    def __init__(self, name, code):
        super(MonthDataCurve_DifBar, self).__init__()
        self.resize(1000, 600)
        self.setWindowTitle('{}({}) 月波动值（最高涨、最低跌之差）'.format(name, code))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setObjectName("gridLayout")

    @CatchExceptions
    def showCurve(self, x, y):
        self.x = x[::-1]
        self.y = y[::-1]

        series = QBarSeries()
        self.setTempZ = QBarSet('净值差值')
        self.setTempZ.hovered.connect(self.onBarSetClicked)
        self.setTempZ.append(self.y)
        self.setTempZ.setColor(QColor('#E14745'))
        series.append(self.setTempZ)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("月净值差值(月末减月初，该月是否盈亏）")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        axisY = QValueAxis()
        axisY.applyNiceNumbers()
        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        self.axis_x = QBarCategoryAxis()
        self.axis_x.append(self.x)
        self.axis_x.setLabelsAngle(75)
        chart.addAxis(self.axis_x, Qt.AlignBottom)
        series.attachAxis(self.axis_x)

        # 隐藏刻度线
        self.axis_x.setGridLineVisible(False)
        axisY.setGridLineVisible(False)

        chart.legend().setVisible(True)
        # chart.legend().setAlignment(Qt.AlignBottom)

        self.chartView = QChartView(chart)
        self.chartView.setRenderHint(QPainter.Antialiasing)
        self.gridLayout.addWidget(self.chartView, 0, 0, 1, 1)
        self.exec()

    @CatchExceptions
    def onBarSetClicked(self, status, index):
        if status:
            text = "{}\n净值差值元：{}%".format(self.x[index], self.y[index])

            QToolTip.showText(QCursor.pos(), text, self, self.rect(), 2000)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mlist, startlist, endlist, maxlist, minlist, reslist = fundMonthData()
    login = MonthDataCurve_DifBar('', '')
    login.resize(1000, 600)
    login.showCurve(mlist, reslist)
    sys.exit(app.exec_())

